package interview.hw_od;

import java.util.HashMap;
import java.util.Scanner;

/**
 *  阿里巴巴找黄金宝箱(III) https://codefun2000.com/p/P1331
 */
public class AlibabaGoldBox3 {

    /**
     * 用一个HashMap记录每个元素的位置，若找到了符合的重复间距，则打印并返回
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] box = sc.nextLine().split(",");
        int d = Integer.parseInt(sc.nextLine());
        HashMap<String, Integer> map = new HashMap<>();
        for (int i = 0;i < box.length;i++) {
            //若找到了符合的重复间距，则打印并返回
            if (map.containsKey(box[i]) && i - map.get(box[i]) <= d) {
                System.out.println(map.get(box[i]));
                return;
            }
            //更新元素位置
            map.put(box[i], i);
        }
        System.out.println(-1);
    }
}
